<template>
  <div>
    <!-- 搜索 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="110" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable ref="table" :columns="columns" :params="fetchParams" :fetchapi="fetchapi" :datakey="'records'" :onColumnsChange="columns => (this.columns = columns)" :isSelectColumn="false">
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" icon="el-icon-plus" :click="exportFileBtn.bind()">导出 </multiuse-button>
      </template>
    </FilterTable>
    <BaseDialog :visible.sync="visibleAdd" :title="'选择经销商'" destroyOnClose width="850px" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <DealerList @close="closeAdd" @save="saveAdd"></DealerList>
    </BaseDialog>
  </div>
</template>
<script>
import DealerList from '@/pages/claimDemage/tools/DealerList.vue';
import { getPreDestoryListByPage, getListForExport,exporturl} from '@/api/usedPartsDestructionFactory/ToBeDestroyed';
import { exportExcelFile } from '@/utils/index';
import moment from "moment";
import { getAppId } from '@/assets/js/auth';

export default {
  components: {
    DealerList
  },
  data() {
    return {
      dataList: [],
      visible: false,
      visibleDrawer: false,
      collapse: false,
      infoId: null,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      columns: this.creatHeader(),
      // fetchApi: getUser,
      fetchParams: {
        xhrAbort: true
      }, //ipa 接口
      selectedRows: [], //选中经销商
      dialogTitle: '选择车辆',
      visibleAdd: false,
      fetchapi: getPreDestoryListByPage,
      dealer: {}
    };
  },
  created() {
    this.fetchParams.beginApplyAt = moment().startOf('week').format('YYYY-MM-DD HH:mm:ss');
    this.fetchParams.endApplyAt = moment().endOf('week').format('YYYY-MM-DD HH:mm:ss');
  },
  methods: {
    close() {
      this.visible = false;
    },
    chooseOrderNo() {
      this.visibleAdd = true;
    },
    async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000167' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // let beginSubmitDate = this.topFilterInit.find(x => x.fieldName == "beginApplyAt|endApplyAt").initialValue[0];
      // let endSubmitDate = this.topFilterInit.find(x => x.fieldName == "beginApplyAt|endApplyAt").initialValue[1];
      // if(!beginSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '索赔单审核开始日期不可为空！' });
      //   return
      // }
      // if(!endSubmitDate) {
      //   this.$notify.warning({ title: '提示信息', message: '索赔单审核结束日期不可为空！' });
      //   return
      // }
      // if(getAppId() == 'arcfox') {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '索赔单审核日期跨度不可超过十二个月！' });
      //     return
      //   }
      // } else {
      //   if (moment(moment(endSubmitDate).format('YYYY-MM-DD')).diff(moment(beginSubmitDate), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '索赔单审核日期跨度不可超过十二个月！' });
      //     return
      //   }
      // }
      // let obj = Object.assign({}, this.fetchParams, {xhrAbort: false});
      // obj.beginApplyAt = this.topFilterInit.find(x => x.fieldName == "beginApplyAt|endApplyAt").initialValue[0];
      // obj.endApplyAt = this.topFilterInit.find(x => x.fieldName == "beginApplyAt|endApplyAt").initialValue[1];
      // getListForExport(obj).then(res => {
      //   exportExcelFile(res.data, '经销商待销毁旧件查询');
      // });
    },
    add() {
      this.visibleAdd = true;
    },
    closeAdd() {
      this.visibleAdd = false;
    },
    saveAdd(val) {
      this.topFilterInit.find(x => x.fieldName == 'ownerCode').initialValue = val[0].dealerCode;
      this.dealer = { ...val[0] };
      this.visibleAdd = false;
    },
    //搜索获取数据
    changeHandle(val) {
      if(!val.beginApplyAt) {
        this.$notify.warning({ title: '提示信息', message: '索赔单审核开始日期不可为空！' });
        return
      }
      if(!val.endApplyAt) {
        this.$notify.warning({ title: '提示信息', message: '索赔单审核结束日期不可为空！' });
        return
      }
      if(getAppId() == 'arcfox') {
        if (moment(moment(val.endApplyAt).format('YYYY-MM-DD')).diff(moment(val.beginApplyAt), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '索赔单审核日期跨度不可超过十二个月！' });
          return
        }
      } else {
        if (moment(moment(val.endApplyAt).format('YYYY-MM-DD')).diff(moment(val.beginApplyAt), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '索赔单审核日期跨度不可超过十二个月！' });
          return
        }
      }
      this.fetchParams = Object.assign({}, this.fetchParams, val,{xhrAbort: false});
    },
    //搜索数据
    topFilterInitFun() {
      return [
        {
          type: 'INPUT',
          label: '选择经销商',
          fieldName: 'ownerCode',
          placeholder: '请输入',
          initialValue: '',
          readonly: true,
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseOrderNo()} />
              </div>
            );
          }
        },
        { type: 'INPUT', label: '旧件代码', fieldName: 'partCode', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '旧件名称', fieldName: 'partName', placeholder: '请输入', initialValue: '' },
        {
          type: 'SELECT',
          label: '索赔单号',
          fieldName: 'claimNo',
          placeholder: '请输入',
          initialValue: '',
          itemList: [{ text: '保存', value: '1001' }]
        },
        { type: 'RANGE_DATE', label: '索赔单审核时间', fieldName: 'beginApplyAt|endApplyAt', placeholder: '', initialValue: [
            moment()
              .startOf('week')
              .format('YYYY-MM-DD HH:mm:ss'),
            moment()
              .endOf('week')
              .format('YYYY-MM-DD HH:mm:ss')
          ],
          rules: [{ required: true, message: '请选择日期', trigger: 'change' }]
        },
        { type: 'INPUT', label: 'VIN', fieldName: 'vin', placeholder: '请输入', initialValue: '' }
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '经销商代码', dataIndex: 'ownerCode', sorter: true },
        { title: '经销商名称', dataIndex: 'ownerName', sorter: true },
        { title: '索赔单号', dataIndex: 'claimNo', sorter: true },
        { title: '维修工单号', dataIndex: 'roNo', sorter: true },
        { title: '索赔类型', dataIndex: 'cmsClaimTypeStr', sorter: true },
        { title: 'VIN', dataIndex: 'vin', sorter: true },
        { title: '旧件代码', dataIndex: 'partCode', sorter: true },
        { title: '旧件名称', dataIndex: 'partName', sorter: true },
        { title: '是否主损件', dataIndex: 'isMain', sorter: true },
        { title: '旧件数量', dataIndex: 'partCount', sorter: true },
        { title: '待销毁数量', dataIndex: 'inventoryQuantity', sorter: true },
        { title: '主损件代码', dataIndex: 'mainPartCode', sorter: true },
        { title: '主损件名称', dataIndex: 'mainPartName', sorter: true },
        { title: '索赔审核时间', dataIndex: 'checkAt', sorter: true }
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
